package org.azdroid.kit.util;

import android.content.Context;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * @author Steven.Li
 *
 */
public final class LocationHelper {

	public static LocationClient mLocationClient;
	static final String TAG = "LocationHelper";
	private static final String AK = "dyukZ9ZsKaYRFZSpwOHxZxyZ";
	
	final public static int requestLocation(Context context,BDLocationListener listener){
		if(mLocationClient == null){
			mLocationClient = new LocationClient(context.getApplicationContext());     //声明LocationClient类
			mLocationClient.setAK(AK);
		}
		mLocationClient.registerLocationListener( listener ); 
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");//返回的定位结果包含地址信息
		option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(500);//设置发起定位请求的间隔时间为500ms
		option.setPriority(LocationClientOption.GpsFirst);
		option.setProdName("hanlinjingwei");
		option.setTimeOut(55000);
		option.disableCache(true);//禁止启用缓存定位
		mLocationClient.setLocOption(option);
		if(!mLocationClient.isStarted()){
			mLocationClient.start();
		}
		return mLocationClient.requestLocation();
	}
	final public static int requestLocationAsNet(Context context,BDLocationListener listener){
		if(mLocationClient == null){
			mLocationClient = new LocationClient(context.getApplicationContext());     //声明LocationClient类
			mLocationClient.setAK(AK);
		}
		mLocationClient.registerLocationListener( listener );    //注册监听函数
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(5000);
		option.setTimeOut(55000);
		option.disableCache(true);//禁止启用缓存定位
		mLocationClient.setLocOption(option);
		if(!mLocationClient.isStarted()){
			mLocationClient.start();
		}
		return mLocationClient.requestLocation();
	}
	
	
	/**
	 * 调用requestLocation获取位置信息后 执行该方法取消监听事件
	 * @param listener
	 */
	final public static void unRegisterListener(BDLocationListener listener){
		if(mLocationClient != null){
			mLocationClient.unRegisterLocationListener(listener);
		}
	}
	
	/**
	 * 调用requestLocation获取位置信息后 执行该方法停止LocationClient
	 */
	public static void stopRequestLocation(){
		if(mLocationClient == null){
			return;
		}else{
			if(mLocationClient.isStarted())
				mLocationClient.stop();
		}
	}
}
